/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.searchguigearbox.GUI;

import com.compomics.searchguigearbox.gearbox.GearBox;
import com.compomics.searchguigearbox.processmanager.MainProcessManager;
import com.compomics.searchguigearbox.processmanager.enums.PriorityLevel;
import com.compomics.searchguigearbox.processmanager.interfaces.ProcessManager;
import java.awt.Dialog;
import java.lang.management.ManagementFactory;
import javax.swing.JOptionPane;

/**
 *
 * @author Kenneth
 */
public class GearBoxGUI extends javax.swing.JFrame {

    /**
     * Creates new form GearBoxGUI
     */
    private GearBox gearbox = GearBox.getInstance();

    public GearBoxGUI() {
        initComponents();
        gearbox.addProcess("omssacl.exe");
        gearbox.addProcess("tamdem.exe");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        GearDown = new javax.swing.JButton();
        GearUp = new javax.swing.JButton();
        firstLabel = new javax.swing.JLabel();
        gearLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SearchGUI GearBox 1.0");

        jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Kenneth\\Pictures\\gearbox.png")); // NOI18N

        GearDown.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        GearDown.setText("DOWN");
        GearDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GearDownActionPerformed(evt);
            }
        });

        GearUp.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        GearUp.setText("UP");
        GearUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GearUpActionPerformed(evt);
            }
        });

        firstLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        firstLabel.setText("Current gear : ");

        gearLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        gearLabel.setText("NORMAL");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(GearDown)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(GearUp, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(firstLabel)
                    .addComponent(gearLabel))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(firstLabel)
                .addGap(18, 18, 18)
                .addComponent(gearLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(GearDown)
                    .addComponent(GearUp))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void GearUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GearUpActionPerformed
        if (warn()) {
            gearbox.changeGear("UP");
            gearLabel.setText(gearbox.getPriorityLevel().name());
        }
    }//GEN-LAST:event_GearUpActionPerformed

    private void GearDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GearDownActionPerformed
        if (!gearbox.getPriorityLevel().equals(PriorityLevel.IDLE)) {
            gearbox.changeGear("DOWN");
            gearLabel.setText(gearbox.getPriorityLevel().name());
        }
    }//GEN-LAST:event_GearDownActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Metal".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GearBoxGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GearBoxGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GearBoxGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GearBoxGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GearBoxGUI().setVisible(true);
                //give own process high priority !
                ProcessManager manager = MainProcessManager.getPriorityManager();
                String[] processName = ManagementFactory.getRuntimeMXBean().getName().split("@");
                int PID = Integer.parseInt(processName[0]);
                manager.setPriorityFromPID(PID, PriorityLevel.REAL_TIME);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton GearDown;
    private javax.swing.JButton GearUp;
    private javax.swing.JLabel firstLabel;
    private javax.swing.JLabel gearLabel;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

    private boolean warn() {
        if (PriorityLevel.isIntensive(gearbox.getPriorityLevel().getNext())) {
            JOptionPane warningDialog = new JOptionPane();
            int dialogResult = JOptionPane.showConfirmDialog(this,
                    "Warning : Any priority above 'BELOW_NORMAL' will result into a very intensive use of the CPU ! Do you wish to continue?",
                    "WARNING",
                    JOptionPane.YES_NO_OPTION);

            if (dialogResult == JOptionPane.YES_OPTION) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
}
